package com.gz.im.backend.component.handler;

import cn.hutool.core.convert.Convert;
import com.gz.im.backend.component.DlgInterface;
import com.gz.im.backend.component.manage.LocalCacheManage;
import com.gz.im.backend.component.manage.RedissonManage;
import com.gz.im.backend.mapper.entity.DialogNodeCondition;
import com.gz.im.backend.pojo.MessageRequest;
import com.gz.im.backend.pojo.User;
import com.gz.im.backend.pojo.enums.OperateTypeEnum;
import com.gz.im.backend.service.DialogNodeService;
import com.gz.im.backend.service.strategy.OperateStrategy;
import com.gz.im.backend.service.strategy.OperateStrategyFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @author GuoZhong
 * @description 机器人处理类
 * @date 2023/3/28 13:20
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class RobotHandler {

	private final LocalCacheManage localCacheManage;

	private final DialogNodeService dialogNodeService;

	private final OperateStrategyFactory operateStrategyFactory;


    /**
     * 匹配节点
     * @param messageRequest
     */
	public String matchNode(MessageRequest messageRequest){
		//匹配话术
		String text= doMatchSpeech(messageRequest,(e)->{
			//匹配操作进行回调真正操作
			doMatchOperate(e,messageRequest);
		});
		return text;
	}

	private String doMatchSpeech(MessageRequest messageRequest, DlgInterface dlgInterface) {
		log.info("doMatchSpeech start-messageRequest:{}",messageRequest);
		//从本地缓存获取当前ID
		User userRelClient = localCacheManage.getUser(messageRequest.getSenderId());
		Long node = userRelClient.getCurrentNodeId();
		//初始化 文本与操作类型
		String speechText="抱歉，没有理解你的意思，麻烦再说一遍";
		Integer operateType=null;
		//获取节点ID条件信息，按照节点条件排序，为null在下面
		List<DialogNodeCondition> nodeConditionList=dialogNodeService.getNodeConditionByNodeId(node);
		for (DialogNodeCondition e:nodeConditionList) {
			String nodeCondition = e.getNodeCondition();
			//条件不为空，先判断有条件的，是否满足
			if(StringUtils.isNotBlank(nodeCondition) ){
				//转成字符中
				String[] conditionArr = nodeCondition.split("#");
				List<String> conditionList = (List<String>) Convert.toList(conditionArr);
				//完全匹配
				boolean flag = doLikeMatchNode(messageRequest.getContent(), conditionList);
				if(flag){
					speechText=dialogNodeService.getNodeContent(e.getToNodeId());
					operateType=e.getOperateType();
					userRelClient.setCurrentNodeId(e.getToNodeId());
					break;
				}
				//条件为空,没有条件，直接匹配
			}else{
				speechText=dialogNodeService.getNodeContent(e.getToNodeId());
				operateType=e.getOperateType();
				userRelClient.setCurrentNodeId(e.getToNodeId());
			}
		}

		log.info("doMatchSpeech end-speechText:{},operateType:{}",speechText,operateType);

		//匹配操作 进行回调
		dlgInterface.doMatchOperate(operateType);
		return speechText;

	}
	/**
	 * 处理命令操作
	 *
	 * @param operateAction
	 * @param messageRequest
	 */
	private void doMatchOperate( Integer operateAction, MessageRequest messageRequest) {
		log.info("doMatchOperate operateType:{}",operateAction);
		String descByType = OperateTypeEnum.getDescByType(operateAction);
		if(null!=descByType){
			//执行操作
			OperateStrategy resource = operateStrategyFactory.getResource(descByType);
			if (null != resource) {
				resource.doOperate(messageRequest);
			}
		}
	}
	/**
	 * 是否匹配节点(完全匹配)
	 * @param content
	 * @return
	 */
	public boolean doFullMatchNode(String content,List<String>conditionList){
		boolean result = conditionList.stream().anyMatch(e -> Objects.equals(e,content));
		return result;
	}

	/**
	 * 是否匹配节点(模糊匹配)
	 * @param content
	 * @return
	 */
	public boolean doLikeMatchNode(String content,List<String>conditionList){
		boolean result = conditionList.stream().anyMatch(e -> content.contains(e));
		return result;
	}

}
